﻿using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class PathLine
{
    public List<Vector2> points;
}
public class GraphicPath
{
    private List<Vector3> points = new List<Vector3>();
    public List<Vector3> path => points;


    // 将当前点移动到指定坐标
    public void MoveTo(float x,float y)
    {
        Vector2 point = new Vector2(x, y);
        points.Add(point);
    }

    // 添加贝塞尔曲线
    //public void CubicTo(Vector3 controlPoint1, Vector3 controlPoint2, Vector3 endPoint)
    internal void CubicTo(float x1, float y1, float x2, float y2, float x3, float y3)
    {
        var controlPoint1 = new Vector2(x1, y1);
        var controlPoint2 = new Vector2(x2, y2);
        var endPoint = new Vector2(x3, y3);
        points.AddRange(BezierCurve(points[points.Count - 1], controlPoint1, controlPoint2, endPoint));
    }

    // 添加直线
    //public void LineTo(Vector3 endPoint)
    internal void LineTo(float x, float y)
    {
        var endPoint = new Vector2(x, y);

        points.Add(endPoint);
    }

    // 获取绘制路径的所有点
    //public Vector3[] GetPoints() 
    //{
    //    return points.ToArray();
    //}

    //// 绘制路径
    //public void DrawPath()
    //{
    //    if (points.Count < 2)
    //        return;

    //    for (int i = 1; i < points.Count; i++)
    //    {
    //        Debug.DrawLine(points[i - 1], points[i], Color.red);
    //    }
    //}

    // 贝塞尔曲线插值算法
    private List<Vector3> BezierCurve(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3)
    {
        List<Vector3> curvePoints = new List<Vector3>();

        for (float t = 0; t <= 1; t += 0.05f)
        {
            float u = 1 - t;
            float tt = t * t;
            float uu = u * u;
            float uuu = uu * u;
            float ttt = tt * t;

            Vector3 point = uuu * p0 + 3 * uu * t * p1 + 3 * u * tt * p2 + ttt * p3;
            curvePoints.Add(point);
        }

        return curvePoints;
    }
    internal void Reset()
    {
        points.Clear();
    }

    //List<PathLine> path = new List<PathLine>();
    //public List<PathLine> Path
    //{
    //    get
    //    {
    //        return path;
    //    }
    //}

    //internal void CubicTo(float x1, float y1, float x2, float y2, float x3, float y3)
    //{
    //    var p = new List<Vector2>();
    //    p.Add(new Vector2(x1, y1));
    //    p.Add(new Vector2(x2, y2));
    //    p.Add(new Vector2(x3, y3));

    //    this.path.Add(new PathLine() { points = p });
    //}

    //internal void LineTo(float tmpX2, float tmpY2)
    //{
    //    if (path.Count > 0)
    //    {
    //        path[path.Count - 1].points.Add(new Vector2(tmpX2, tmpY2));
    //    }
    //    else
    //    {
    //        path.Add(new PathLine() {points = new List<Vector2> { new Vector2(tmpX2, tmpY2) } });
    //    }
    //}

    //internal void MoveTo(float tmpX1, float tmpY1)
    //{
    //    bool move = false;
    //    if (path.Count > 0)
    //    {
    //        if (path[path.Count - 1].points.Count == 1)
    //        {
    //            move = true;
    //            path[path.Count - 1].points[0] = new Vector2(tmpX1, tmpY1);
    //        }
    //    }
    //    if(!move)
    //    {
    //        path.Add(new PathLine() { points = new List<Vector2> { new Vector2(tmpX1, tmpY1) } });
    //    }
    //}

}